<template>
    <div class="registeredUsers-wrap">
        <div class="registeredUsers-login">
            <div class="registeredUsers-title">忘记密码</div>
            <el-form :model="param" :rules="rules" ref="RegisteredUsers" label-width="0px" class="registeredUsers-content">
                <el-form-item prop="mailbox">
                    <el-input placeholder="请输入邮箱" v-model="param.mailbox" class="input-with-select">
                     <el-button v-if="state==0" slot="append" @click="sendemail">验证</el-button>
                     <el-button v-else slot="append" disabled>{{time}}</el-button>
                    </el-input>
                </el-form-item>

                <el-form-item prop="check">
                    <el-input v-model="param.check" placeholder="请输入验证码"> </el-input>
                </el-form-item>

                <el-form-item prop="password">
                    <el-input type="password" placeholder="请输入密码" v-model="param.password"     >
                    </el-input>
                </el-form-item>
                <el-form-item prop="password2">
                    <el-input type="password" placeholder="请再次确认密码" v-model="param.password2" >
                    </el-input>
                </el-form-item>
                <div class="login-btn">
                    <el-button type="primary" @click="submitForm()">提交</el-button>
                </div>
                <p class="login-tips">Tips : 通过邮箱重新设置密码</p>
            </el-form>
        </div>
    </div>
</template>

<script>
import {
    sendMail,
    forgetpwd,
    verifyemail
    } from "../../api/register"
export default {
    data: function () {
      // 密码的校验方法
      let validatePass = (rule, value, callback) => {
        if (value === "") {
          return callback(new Error("请输入密码"));
        }
        // 密码以字母开头,长度在6-18之间,允许字母数字和下划线
        const passwordRule = /^[a-zA-Z]\w{5,17}$/;
        if (passwordRule.test(value)) {
          this.$refs.RegisteredUsers.validateField("checkPass");
          return callback();
        } else {
          return callback(
            new Error("字母开头,长度6-18之间,允许字母数字和下划线")
          );
        }
      };
      let validatePass2 = (rule, value, callback) => {
        if (value === "") {
          return callback(new Error("请再次确认密码"));
        }
        // 密码以字母开头,长度在6-18之间,允许字母数字和下划线
        const passwordRule = /^[a-zA-Z]\w{5,17}$/;
        if (passwordRule.test(value)) {
          this.$refs.RegisteredUsers.validateField("checkPass");
          return callback();
        } else {
          return callback(
            new Error("字母开头,长度6-18之间,允许字母数字和下划线")
          );
        }
      };
      //检测邮箱是否注册
      let validateemail = (rule, value, callback) => {
          console.log("xxxx")
        if (value === "") {
          return callback(new Error("请输入邮箱"));
        }
        const emailRule = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
        if (emailRule.test(value)) {
            console.log("4445")
         verifyemail({
                 email:this.param.mailbox
             })
         .then((res)=>{
             console.log(res)
             if(res.code != "200"){
                this.$refs.RegisteredUsers.validateField("checkPass");
                 return callback();
             }else{
                 return callback(new Error("此邮箱未注册，请注册后登录"))
             }
         })
        } else {
          return callback(new Error("格式xxxx@xx.xxx"));
        }
      };
        return {
            yzm:"",
            state:0,
            time:60,
            param: {
                password: '',
                mailbox:"",
                check:"",
                password2:""
            },
            rules: {
                // username: [{ validator: validateName, trigger: "blur" }],
                password: [{ validator: validatePass, trigger: "blur" }],
                password2: [{ validator: validatePass2, trigger: "blur" }],
                mailbox:[{ validator: validateemail, trigger: "blur" }],
                check:[{ required: true, message: '请输入验证码', trigger: 'blur' }]
            }
        };
    },
    methods: {
        submitForm() {
            if (this.param.check != this.yzm) {
                this.$message.error('验证码输入错误');
                if(this.param.password!=this.param.password2){
                 this.$message.error('两次输入密码不一致');
                }
            } else {
                this.$refs.RegisteredUsers.validate((valid) => {
                    if (valid) {
                        forgetpwd({
                            mailbox: this.param.mailbox,
                            password: this.param.password
                        })
                        .then((res)=>{
                            console.log(res.mess)
                            this.$message.success(res.mess)
                            this.$router.push("/login")
                        })
                        .catch((res)=>{
                            this.$message.error(res.mess)
                        })

                        
                    }
                });
            }
        },
        //发送给邮箱验证码
        sendemail(){
            //设置定时器
            this.state = 1;
            var m = setInterval(() => {
                if (this.time == 0) {
                    this.state=0
                    clearInterval(m);
                    this.time=60
                } else {
                    this.time-=1
                }
            }, 1000);
            console.log("发送邮件");
            console.log(this.param.mailbox);     
            sendMail({
                email:this.param.mailbox
            })
            .then((res)=>{
                console.log(res.data);
              this.yzm = res.data
            })
        }
    }
};
</script>

<style scoped>
.registeredUsers-wrap {
    position: relative;
    width: 100%;
    height: 100vh;
    background-image: url(../../assets/img/7.jpg);
    background-size: 100%;
}
.registeredUsers-title {
    width: 100%;
    line-height: 50px;
    text-align: center;
    font-size: 20px;
    color: #fff;
    border-bottom: 1px solid #ddd;
}
.registeredUsers-login {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 350px;
    margin: -190px 0 0 -175px;
    border-radius: 5px;
    background: rgba(255, 255, 255, 0.3);
    overflow: hidden;
}
.registeredUsers-content {
    padding: 30px 30px;
}
.login-btn {
    text-align: center;
}
.login-btn button {
    width: 100%;
    height: 36px;
    margin-bottom: 10px;
}
.login-tips {
    font-size: 12px;
    line-height: 30px;
    color: #fff;
}
</style>